/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package odeoncinemaassignment;

import java.sql.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class utilities {
    static DecimalFormat df = new DecimalFormat("##.##");
    // This class contains utilities for all jframes to use, so as to maximise code reuse
    private static String DATE_FORMAT_NOW;

    public static void exitApp() {
        // used throughout application to exit .
        // all error handling

        int exit = JOptionPane.showConfirmDialog(null, "Are you sure You Want To Exit?");
        if (exit == JOptionPane.YES_OPTION) {
            try {
                System.exit(exit);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "An Exception Occured: " + e.getMessage());
            }
        }
        if (exit == JOptionPane.NO_OPTION) {
            JOptionPane.showMessageDialog(null, "Returning to Program!");
        }
        if (exit == JOptionPane.CANCEL_OPTION) {
            JOptionPane.showMessageDialog(null, "Returning to Program!");
        }

    }
    public static String dateFormat(Date unformatedDate){
        // this method takes a date object and converts to string
        String formatedDate=""; // string to hold the formatted date object
        try{
        
        SimpleDateFormat formatter;   // simple date format to format date output
        formatter = new SimpleDateFormat("dd-MM-yyyy");
        formatedDate = formatter.format(unformatedDate);   // apply format to value from date picker
        
        
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Please Choose a Date from the date picker.","Error, Please select Date",JOptionPane.ERROR_MESSAGE);
            
        }
        return  formatedDate;
    }
    public static String now() {
        // method uses calendar class to get current date and time for status messages
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        return sdf.format(cal.getTime());
// method used for timestamp
    }
    public static void exitToMenu(){
           int exit = JOptionPane.showConfirmDialog(null, "Are you sure You Want To Exit To Main Menu?");
        if (exit == JOptionPane.YES_OPTION) {
            try {
                homeScreenFrame newHomeScreenFrame = new homeScreenFrame(); //create instance of frame called newFrame
                newHomeScreenFrame.setVisible(true); //set it visible
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "An Exception Occured: " + e.getMessage());
            }
        }
        if (exit == JOptionPane.NO_OPTION) {
            JOptionPane.showMessageDialog(null, "Returning to Program!");
        }
        if (exit == JOptionPane.CANCEL_OPTION) {
            JOptionPane.showMessageDialog(null, "Returning to Program!");
        }
    }
   
   
    public static Connection connectDB(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/odeon","root","");
        JOptionPane.showMessageDialog(null, "Database Connection Successfull! Connected to "+conn.getCatalog());
        return conn;
    }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Database Connection Error! \nDetails: \n"+e);
            return null;
        }
    }
    
    // below method can be called to populate jlists!
    public static void populateJList(JList list, String query, Connection connection) throws SQLException{
    DefaultListModel model = new DefaultListModel(); //create a new list model
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery(query); //run your query
    while (resultSet.next()) //go through each row that your query returns
    {
        String title = resultSet.getString("title"); //get the element in column "title"
        model.addElement(title); //add each item to the model
    }
    list.setModel(model);
    resultSet.close();
    statement.close();
}
    public static ResultSet getResultSet(String sql, Connection conn)throws SQLException{
        Statement stmnt = conn.createStatement();
        ResultSet rs = stmnt.executeQuery(sql);
        return rs;
        // returns resultset so objects of type movie can be created. 
    }
    
     
 
    
}